package com.lz.football.module.ladderMatch
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.liangzhong.events.LadderEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.ladderMatch.vo.MatchTimeVo;
	import com.lz.football.module.base.IComponent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.LButton;

	/**
	 *天梯操作
	 *  
	 * @author Administrator
	 * 
	 */	
	public class LadderoperationView extends Sprite implements IComponent
	{
		private var _skin:MovieClip;
		/**
		 *取消比赛 
		 */
		private var cancelBtn:SimpleButton;
		/**
		 *报名比赛 
		 */
		private var applyBtn:SimpleButton;

		/**
		 *比赛时间 
		 */
		private var time:TextField;
		/**
		 *时间说明  
		 */
		private var timeDec:TextField;
		/**
		 *自动报名  
		 */
		private var atuoBtn:SimpleButton;
		/**
		 * 自动取消报名 
		 */
		private var cancelAutobtn:SimpleButton;
		
		
		/**
		 * 
		 */
		private var _time:int;
		/**
		 *选择阵容mc 
		 */		
		private var selectLineUpMc :MovieClip;
		/**
		 *阵容选择下拉 
		 */		
		private var lineUpCombMc	:MovieClip;
		/**
		 *选择的阵容索引 
		 */		
		private var lineUpIndex	:int = 1;
		
		/**
		 *是否可以天梯比赛 
		 */
		private var _isSureMatch:Boolean;
		
		public function LadderoperationView()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = view as MovieClip;
			cancelBtn = _skin.cancelBtn;
//			exchangeBtn = _skin.exchangeBtn;
//			ruleBtn = _skin.ruleBtn;
			time = _skin.time;
			time.mouseEnabled = false;
			timeDec = _skin.timeDec;
			timeDec.mouseEnabled = false;
			applyBtn = _skin.applyBtn;
			atuoBtn = _skin.atuoBtn//new LButton(_skin.atuoBtn);
			cancelBtn.visible = false;
			cancelAutobtn = _skin.cancelAutobtn;
			cancelAutobtn.visible = false;
			lineUpCombMc = Reflection.createInstance("com.football.ladderMatch.lineUpCombSkin");
			selectLineUpMc = _skin.selectLineUpMc;
			selectLineUpMc.buttonMode = true;
			selectLineUpMc.txtName.mouseEnabled = false;
			time.text = "";
			timeDec.text = "";
			checkTimeIsSureMatch();
			addEvtListener();
			setHide();
			updataStatus();
		}
		/**
		 * 1.自动报名
		 * 2.取消自动报名
		 * 3.手动报名
		 * 4.手动取消报名  
		 * @param $obj
		 * 
		 */		
		public function matchStatus($obj:Object):void
		{
			setHide();
			switch($obj.type)
			{
				case 1:
					cancelAutobtn.visible = true;
					cancelBtn.visible = true;
					lineUpIndex =  AppData.getInstance().ladderDataManger.ladderInfovo.usebattle
					calculateTime();
					break;
				case 2:
					atuoBtn.visible = true;
					cancelBtn.visible = true;
					break;
				case 3:
					cancelBtn.visible = true;
					atuoBtn.visible = true;
					calculateTime();
					break;
				case 4:
					updataStatus();
					TimerManager.instance.removeFunction(updataTime);
					break;
					
			}
			if(!$obj.status)
			{
				TimerManager.instance.removeFunction(updataTime);
				atuoBtn.visible = true;
				applyBtn.visible = true;
				time.text = "";
				timeDec.text = "";
			}
		}
		/**
		 *设置隐藏 
		 * 
		 */
		private function setHide():void
		{
			applyBtn.visible = false;
			cancelBtn.visible = false;
			cancelAutobtn.visible = false;
			atuoBtn.visible = false;
		}
		/**
		 *设置天梯比赛状态  
		 *  $status true 自动状态 ，false
		 */
		public function updataStatus():void
		{
			if(AppData.getInstance().ladderDataManger.ladderInfovo.isApplyStatus)
			{
//				
				cancelBtn.visible = true;
				lineUpIndex =  AppData.getInstance().ladderDataManger.ladderInfovo.usebattle
				if( 1== lineUpIndex)
				{
					selectLineUpMc.txtName.text = "首发阵容";
				}
				else
				{
					selectLineUpMc.txtName.text = "第二阵容";
				}
				calculateTime();
				if(AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch)
				{
					cancelAutobtn.visible = true;
					atuoBtn.visible = false
				}else
				{
					cancelAutobtn.visible = false;
					atuoBtn.visible = true;
				}
			}else
			{
				atuoBtn.visible = true;
				applyBtn.visible = true;
				time.text = "";
				timeDec.text = "";
			}
		}
		/**
		 *天梯比赛开始或者结束 
		 * 
		 */
		public function ladderMatchStartOrFinish($obj:Object):void
		{
			setHide();
			checkTimeIsSureMatch();
			TimerManager.instance.removeFunction(updataTime);
			if(_isSureMatch)
			{
				updataStatus();
				
				if(AppData.getInstance().ladderDataManger.ladderInfovo.laddercount <= 0 &&　AppData.getInstance().ladderDataManger.ladderInfovo.isApplyStatus)
				{
					Alert.showErrorTip("挑战次数不够");
					TimerManager.instance.removeFunction(updataTime);
					atuoBtn.visible = true;
					applyBtn.visible = true;
					time.text = "";
					timeDec.text = "";
					AppData.getInstance().ladderDataManger.ladderInfovo.isApplyStatus = false
					return;
				}
				if(!_isSureMatch)
				{
					Alert.showErrorTip("比赛时间未到");
					return;
				}
				if(AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch)
				{
					_skin.dispatchEvent(new LadderEvent(LadderEvent.APPLY_MATCH,{type:1,ballt:AppData.getInstance().ladderDataManger.ladderInfovo.usebattle},true));
				}
//				if($obj.status)//开始比赛
//					_time +=  10 * 60;
			}else
			{
				atuoBtn.visible = true;
				applyBtn.visible = true;
				time.text = "";
				timeDec.text = "";
			}
			
		}
		/**
		 * 检测时间 是否可以比赛
		 * 
		 */
		private function checkTimeIsSureMatch():void
		{
			var d :Date = AppData.getInstance().curDate;
			var arr:Array = AppData.getInstance().ladderDataManger.matchTimeList;
			var len:int = arr.length;
			var vo:MatchTimeVo;
			_isSureMatch = false;
			for(var i:int = 0;i < len;i++)
			{
				vo = arr[i];
				if(vo.starTime<=d.getHours()&& d.getHours() < vo.endTime )
				{
					_isSureMatch = true;
					break;
				}
			}
		}
		
		/**
		 *计算时间 
		 * 
		 */
		private function calculateTime():void
		{
			var d :Date = AppData.getInstance().curDate;
			var min:int = (d.getMinutes() % 10);
			if(min < 9)
			{
				_time = 10 * 60 -((d.getMinutes() % 10) * 60 + d.getSeconds() + 1);
			}else
			{
				_time = 11 * 60  - d.getSeconds()-1;
			}
			updataTime();
			TimerManager.instance.addFunction(updataTime);
		}
		
		/**
		 *更新时间 
		 * 
		 */		
		private function updataTime():void
		{
			_time--;
			if(_time < 0)
			{
				setHide();
				TimerManager.instance.removeFunction(updataTime);
				atuoBtn.visible = true;
				applyBtn.visible = true;
				
				time.text = "";
				timeDec.text = "";
			}else
			{
				time.text = DateUtil.formatShowHMS(_time,false);
				timeDec.text = "开赛倒计时：";
			}
			
		}
		
		public function addEvtListener():void
		{
			cancelBtn.addEventListener(MouseEvent.CLICK,onCancelHandler);

			applyBtn.addEventListener(MouseEvent.CLICK,onapplyHandler);
			atuoBtn.addEventListener(MouseEvent.CLICK,onAutoHanler);
			cancelAutobtn.addEventListener(MouseEvent.CLICK,oncancelAutoHandler);
			
			selectLineUpMc.addEventListener(MouseEvent.CLICK,lineUpHandler);
			lineUpCombMc.addEventListener(MouseEvent.CLICK,lineUpClickHandler);
		}
		
		/**
		 *取消自动匹配 
		 * @param event
		 * 
		 */
		protected function oncancelAutoHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			_skin.dispatchEvent(new LadderEvent(LadderEvent.APPLY_MATCH,{type:2,ballt:lineUpIndex},true));
		}
		/**
		 *阵容选择 
		 * @param e
		 */		
		private function lineUpHandler(e :MouseEvent):void
		{
			if(lineUpCombMc.parent)
			{
				lineUpCombMc.parent.removeChild(lineUpCombMc);
			}
			else
			{
				_skin.addChild(lineUpCombMc);
				lineUpCombMc.x = selectLineUpMc.x;
				lineUpCombMc.y = selectLineUpMc.y + selectLineUpMc.height +3;
			}
		}
		/**
		 *阵型点击 
		 * @param e
		 * 
		 */		
		private function lineUpClickHandler(e :MouseEvent):void
		{
			if(e.target.name.indexOf("item_") != -1)
			{
				lineUpIndex = e.target.name.split("_")[1];
				if(lineUpIndex == 1)
				{
					selectLineUpMc.txtName.text = "首发阵容";
				}
				else
				{
					selectLineUpMc.txtName.text = "第二阵容";
				}
				if(lineUpCombMc && lineUpCombMc.parent)
				{
					lineUpCombMc.parent.removeChild(lineUpCombMc);
				}
			}
		}
		/**
		 *自动操作 
		 * @param event
		 * 
		 */
		protected function onAutoHanler(event:Event):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().ladderDataManger.ladderInfovo.laddercount <= 0)
			{
				Alert.showErrorTip("挑战次数不够");
				return;
			}
			checkTimeIsSureMatch()
			if(!_isSureMatch)
			{
				Alert.showErrorTip("比赛时间未到");
				return;
			}
			_skin.dispatchEvent(new LadderEvent(LadderEvent.APPLY_MATCH,{type:1,ballt:lineUpIndex},true));
//			calculateTime();
		}
		
		/**
		 *报名比赛 
		 * @param event
		 * 
		 */
		protected function onapplyHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().ladderDataManger.ladderInfovo.laddercount <= 0)
			{
				Alert.showErrorTip("挑战次数不够");
				return;
			}
			checkTimeIsSureMatch()
			if(!_isSureMatch)
			{
				Alert.showErrorTip("比赛时间未到");
				return;
			}
			_skin.dispatchEvent(new LadderEvent(LadderEvent.APPLY_MATCH,{type:3,ballt:lineUpIndex},true));
//			calculateTime();
//			applyBtn.visible = false;
//			cancelBtn.visible = true;
		}
		/**
		 *规则 
		 * @param event
		 * 
		 */		
		protected function onruleHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
		}
		
		/**
		 *兑换 
		 * @param event
		 * 
		 */
		protected function onexchangeHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
		}
		
		/**
		 *取消比赛 
		 * @param event
		 * 
		 */
		protected function onCancelHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
//			if(AppData.getInstance().ladderDataManger.ladderInfovo.isAutoMatch)
//			{
//				_skin.dispatchEvent(new LadderEvent(LadderEvent.APPLY_MATCH,{type:2,ballt:lineUpIndex},true));
//			}else
//			{
				_skin.dispatchEvent(new LadderEvent(LadderEvent.APPLY_MATCH,{type:4,ballt:lineUpIndex},true));
//			}
			
		}
		
		public function removeEvtListener():void
		{
			cancelBtn.removeEventListener(MouseEvent.CLICK,onCancelHandler);
			
			applyBtn.removeEventListener(MouseEvent.CLICK,onapplyHandler);
			atuoBtn.removeEventListener(MouseEvent.CLICK,onAutoHanler);
			cancelAutobtn.removeEventListener(MouseEvent.CLICK,oncancelAutoHandler);
			
			selectLineUpMc.removeEventListener(MouseEvent.CLICK,lineUpHandler);
			lineUpCombMc.removeEventListener(MouseEvent.CLICK,lineUpClickHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			if(lineUpCombMc.parent)lineUpCombMc.parent.removeChild(lineUpCombMc);
			lineUpCombMc = null;
			TimerManager.instance.removeFunction(updataTime);
			if(_skin != null && _skin.parent)
				_skin.parent.removeChild(_skin);
			_skin = null;
		}
	}
}